<?php

namespace CNAM\ActimanBundle\Controller;

use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use CNAM\ActimanBundle\Entity\acProcedure;
use CNAM\ActimanBundle\Form\acProcedureType;
use Symfony\Component\HttpFoundation\Request;
use CNAM\ActimanBundle\Entity\Phase;
use CNAM\ActimanBundle\Form\PhaseType;
use CNAM\ActimanBundle\Entity\Operation;
use CNAM\ActimanBundle\Form\OperationType;
use CNAM\ActimanBundle\Entity\Service;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

class ProcedureController extends Controller {

    public function indexAction() {

        $list_procedure = $this->getDoctrine()
                ->getRepository('CNAMActimanBundle:acProcedure')
                ->findAll();

        $titre = "Procedures";
        
        $em = $this->getDoctrine()->getManager();
        $repository_service = $em->getRepository('CNAMActimanBundle:Service');
        $list_service = $repository_service->findAll();
        
        return $this->render('CNAMActimanBundle:Procedure:listProcedure.html.twig', array('titre' => $titre, 'list_procedure' => $list_procedure, 'list_service' => $list_service));
    }

    public function procedureAddAction() {

        $procedure = new acProcedure();

        $form = $this->createForm(new acProcedureType(), $procedure);
        $procedure->setDateCreation(new \Datetime());
        $procedure->setDateModification(new \Datetime());
        $titre = "Procedure";
        $session = new Session();
        
        $request = $this->get('request');
        if ($request->getMethod = 'POST') {
            $form->bind($request);
        }
        if ($form->isValid()) {
            $procedure->setStatus(true);
            $em = $this->getDoctrine()->getManager();
            $em->persist($procedure);
            $em->flush();

            $phases = $procedure->getPhases();
            $repository_operation = $em->getRepository('CNAMActimanBundle:Operation');
            $Operations = $repository_operation->findAll();   
            
            $session->getFlashBag()->add('success', 'Procédure ajoutée !');
            
            return $this->render('CNAMActimanBundle:Procedure:indexPhase.html.twig', array('titre' => $titre, 'Phases' => $phases, 'Operations' => $Operations, 'Procedure' => $procedure));
        }
        return $this->render('CNAMActimanBundle:Procedure:procedureAdd.html.twig', array('form' => $form->createView(), 'titre' => $titre, 'Procedure' => $procedure));
    }

    public function procedureEditAction($idpro) {
        $procedure = new acProcedure();

        $em = $this->getDoctrine()->getManager();
        $procedureedit = $em->getRepository('CNAMActimanBundle:acProcedure')->find($idpro);

        $form = $this->createForm(new acProcedureType(), $procedure);

        $repository = $this->getDoctrine()
                ->getRepository('CNAMActimanBundle:Phase');

        $criteria = array('acProcedure' => $procedureedit);

        $phases = $repository->findBy($criteria);
        $titre = "Procedure";

        $request = $this->get('request');
        if ($request->getMethod = 'POST') {

            $form->bind($request);
        }
        if ($form->isValid()) {
            $procedureedit->setnomProcedure($procedure->getnomProcedure());
            $procedureedit->setstatus($procedure->getstatus());
            $procedureedit->setservice($procedure->getservice());
            $procedureedit->setuser($procedure->getuser());

            $em = $this->getDoctrine()->getManager();
            $em->persist($procedureedit);
            $em->flush();
            
            $session = new Session();
            $session->getFlashBag()->add('success', 'Procédure modifiée !');
        }
        return $this->render('CNAMActimanBundle:Procedure:procedureEdit.html.twig', array('titre' => $titre, 'Phases' => $phases, "Procedure" => $procedureedit, 'form' => $form->createView()));
    }

    public function procedureDeleteAction($idpro) {
        
        $titre = 'Procedure';

        $em = $this->getDoctrine()->getManager();
        $proceduredelet = $em->getRepository('CNAMActimanBundle:acProcedure')->find($idpro);
        $criteria = array('acProcedure' => $proceduredelet);
        $Phasedelet = $em->getRepository('CNAMActimanBundle:Phase')->findBy($criteria);

        foreach ($Phasedelet as $Phase) {
            $criteria = array('Phase' => $Phase);
            $Operationdelet = $em->getRepository('CNAMActimanBundle:Operation')->findBy($criteria);
            foreach ($Operationdelet as $Operation) {
                $em->remove($Operation);
            }
            $em->remove($Phase);
        }
        $em->remove($proceduredelet);
        $em->flush();

        $procedures = $this->getDoctrine()
                ->getRepository('CNAMActimanBundle:acProcedure')
                ->findAll();
        
        $em = $this->getDoctrine()->getManager();
        $repository_acProcedure = $em->getRepository('CNAMActimanBundle:acProcedure');
        $list_procedure = $repository_acProcedure->findAll();
        $repository_service = $em->getRepository('CNAMActimanBundle:Service');
        $list_service = $repository_service->findAll();
        $session = new Session();
        $session->getFlashBag()->add('warning', 'Procédure supprimée !');
        $twig = $this->container->get('templating');
        $content = $twig->render('CNAMActimanBundle:Procedure:listProcedureEdit.html.twig', array('titre' => $titre, 'Procedures' => $procedures, 'list_service' => $list_service, 'list_procedure' => $list_procedure));
        return new Response($content, 404, array('Content-Type', 'text/html'));
    }

    //Phase

    public function phaseAction($idpro) {

        $em = $this->getDoctrine()->getManager();
        $procedure = $this->getDoctrine()
                ->getRepository('CNAMActimanBundle:acProcedure')
                ->find($idpro);

        $phases = $procedure->getPhases();
 
        $repository_operation = $em->getRepository('CNAMActimanBundle:Operation');
        $Operations = $repository_operation->findAll();

        $titre = "Procedure";
        return $this->render('CNAMActimanBundle:Procedure:indexPhase.html.twig', array('titre' => $titre, 'Phases' => $phases,
            'Operations' => $Operations,
            'Procedure' => $procedure));
    }

    public function phaseAddAction($idpro) {

        $phase = new Phase();
        $form = $this->createForm(new PhaseType(), $phase);

        $procedure = $this->getDoctrine()
                ->getRepository('CNAMActimanBundle:acProcedure')
                ->find($idpro);

        $titre = "Procedure";
        $phase->setAcProcedure($procedure);

        $request = $this->get('request');
        if ($request->getMethod() == 'POST') {
            $form->bind($request);
        }
        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($phase);
            $em->flush();
            $phase = new Phase();
            $session = new Session();
            $session->getFlashBag()->add('success', 'Phase ajoutée !');
        }
        
        $phases = $procedure->getPhases();
        $operations = $phase->getOperations();
        return $this->render('CNAMActimanBundle:Procedure:phaseAdd.html.twig', array('titre' => $titre, 'form' => $form->createView(), 'Phases' => $phases, 'Procedure' => $procedure, 'Phase' => $phase, 'Operations' => $operations));
    }

    public function phaseEditAction($idpha) {
        $Phase = new Phase();

        $em = $this->getDoctrine()->getManager();
        $phaseedit = $em->getRepository('CNAMActimanBundle:Phase')->find($idpha);

        $form = $this->createForm(new PhaseType(), $Phase);


        $operations = $phaseedit->getOperations();
        $titre = "Procedure";

        $request = $this->get('request');
        if ($request->getMethod() == 'POST') {

            $form->bind($request);
        }
        if ($form->isValid()) {
            $phaseedit->setNomPhase($Phase->getNomPhase());

            $em = $this->getDoctrine()->getManager();
            $em->persist($phaseedit);
            $em->flush();
            $session = new Session();
            $session->getFlashBag()->add('success', 'Phase modifiée !');
        }
        
        return $this->render('CNAMActimanBundle:Procedure:phaseEdit.html.twig', array('titre' => $titre, 'form' => $form->createView(), 'Operations' => $operations, 'Phase' => $phaseedit));
    }

    public function phaseDeleteAction($idpha) {
        $titre = 'Procedure';

        $em = $this->getDoctrine()->getManager();
        $phasedelet = $em->getRepository('CNAMActimanBundle:Phase')->find($idpha);

        $Procedure = $phasedelet->getacProcedure();

        $em->remove($phasedelet);
        $em->flush();

        $phases = $Procedure->getPhases();
        
        $repository_operation = $em->getRepository('CNAMActimanBundle:Operation');
        $Operations = $repository_operation->findAll();
        $session = new Session();
        $session->getFlashBag()->add('warning', 'Phase supprimée !');
        $twig = $this->container->get('templating');
        $content = $twig->render('CNAMActimanBundle:Procedure:indexPhase.html.twig', array('titre' => $titre, 'Phases' => $phases, "Procedure" => $Procedure, 'Operations' => $Operations));
        return new Response($content, 404, array('Content-Type', 'text/html'));
    }

    //Operation 

    public function operationAction($idpha) {
        $phase = $this->getDoctrine()
                ->getRepository('CNAMActimanBundle:Phase')
                ->find($idpha);

        $Operations = $phase->getOperations();
        
        $procedure = $phase->getacProcedure();
        
        $titre = "Procedure";
        return $this->render('CNAMActimanBundle:Default:indexoperation.html.twig', array('titre' => $titre, 'Operations' => $Operations, "Phase" => $phase, "Procedure" => $procedure));
    }

    public function operationAddAction($idpha) {
        $titre = "Procedure";
        $phase = $this->getDoctrine()
                ->getRepository('CNAMActimanBundle:Phase')
                ->find($idpha);
        $service = new Service();
        $Operation = new Operation();
        $form = $this->createForm(new OperationType(), $Operation);

        $Operation->setPhase($phase);

        $request = $this->get('request');
        if ($request->getMethod() == 'POST') {

            $form->bind($request);
        }
        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($Operation);
            $em->flush();
            $Operation = new Operation();
            $session = new Session();
            $session->getFlashBag()->add('success', 'Opération ajoutée !');
        }
        
        $Operations = $phase->getOperations();

        return $this->render('CNAMActimanBundle:Procedure:operationAdd.html.twig', array('titre' => $titre, 'form' => $form->createView(), 'Operation' => $Operation, 'Operations' => $Operations, 'Phase' => $phase));
    }

    public function operationEditAction($idop) {
        $em = $this->getDoctrine()->getManager();
        
        $repository_operation = $em->getRepository('CNAMActimanBundle:Operation');
        $Operationedit = $repository_operation->find($idop);

        $form = $this->createForm(new OperationType(), $Operationedit);
        $session = new Session();

        $titre = "Procedure";
        $phase = $Operationedit->getPhase();
        $Operations = $phase->getOperations();

        $request = $this->get('request');
        if ($request->getMethod() == 'POST') {
            $form->bind($request);
            if ($form->isValid()) {
                $em->persist($Operationedit);
                $em->flush();
                $session->getFlashBag()->add('success', 'Opération modifiée !');
            }               
        }
        return $this->render('CNAMActimanBundle:Procedure:operationEdit.html.twig', array('titre' => $titre, 'form' => $form->createView(), 'Operation' => $Operationedit, 'Operations' => $Operations, 'Phase' => $phase));
    }

    public function OperationDeleteAction($idop) {
        $titre = 'Procedure';

        $em = $this->getDoctrine()->getManager();
        $Operationdelet = $em->getRepository('CNAMActimanBundle:Operation')->find($idop);

        $phase = $Operationdelet->getPhase();
        $procedure = $phase->getAcProcedure();
        $list_phase = $procedure->getPhases();
        
        $em->remove($Operationdelet);
        $em->flush();

        $repository_operation = $em->getRepository('CNAMActimanBundle:Operation');
        $Operations = $repository_operation->findAll();
        $session = new Session();
        $session->getFlashBag()->add('warning', 'Opération supprimée !');
        $twig = $this->container->get('templating');
        $content = $twig->render('CNAMActimanBundle:Procedure:indexPhase.html.twig', array('titre' => $titre, 'Operations' => $Operations, "Phases" => $list_phase, "Procedure" => $procedure));
        return new Response($content, 404, array('Content-Type', 'text/html'));
    }
    
    public function menuProcAction() {     
        $em = $this->getDoctrine()->getManager();
        $repository_acProcedure = $em->getRepository('CNAMActimanBundle:acProcedure');
        $list_procedure = $repository_acProcedure->findAll();

        return $this->render('CNAMActimanBundle:Default:layout.html.twig', array('list_procedure' => $list_procedure));
    
    }

    public function listProcedureAction() {
        $em = $this->getDoctrine()->getManager();
        
        $repository_acProcedure = $em->getRepository('CNAMActimanBundle:acProcedure');
        $list_procedure = $repository_acProcedure->findAll();

        $repository_service = $em->getRepository('CNAMActimanBundle:Service');
        $list_service = $repository_service->findAll();
        
        return $this->render('CNAMActimanBundle:Procedure:listProcedure.html.twig', array('list_procedure' => $list_procedure, 'list_service' => $list_service));
    }

    public function listProcedureEditAction() {
        $em = $this->getDoctrine()->getManager();
        
        $repository_acProcedure = $em->getRepository('CNAMActimanBundle:acProcedure');
        $list_procedure = $repository_acProcedure->findAll();

        $repository_service = $em->getRepository('CNAMActimanBundle:Service');
        $list_service = $repository_service->findAll();
        
        return $this->render('CNAMActimanBundle:Procedure:listProcedureEdit.html.twig', array('list_procedure' => $list_procedure, 'list_service' => $list_service));
    }

    public function visuDetailAction($idpro, $idpha) {
        $em = $this->getDoctrine()->getManager();
        
        $repository_acProc = $em->getRepository('CNAMActimanBundle:acProcedure');
        $acProc = $repository_acProc->find($idpro);
        
        $repository_phase = $em->getRepository('CNAMActimanBundle:Phase');
        $list_phase = $repository_phase->findByacProcedure($idpro);

        $repository_operation = $em->getRepository('CNAMActimanBundle:Operation');
        $list_operation = $repository_operation->findAll();


        return $this->render('CNAMActimanBundle:Procedure:visuDetail.html.twig', array('list_phase' => $list_phase, 'list_operation' => $list_operation, 'idpro' => $idpro, 'idpha' => $idpha, 'acProc' => $acProc));
    }

}